InterruptAlert = AceLibrary ("AceAddon-2.0"):new ("AceEvent-2.0")

-- SPELLINTERRUPTOTHEROTHER = "%s interrupts %s's %s.";
-- SPELLINTERRUPTSELFOTHER = "You interrupt %s's %s.";

-- Called upon ADDON_LOADED
function InterruptAlert:OnInitialize ()
  self.frames = {}
  
  self.frames.list = CreateFrame ("Frame", nil, UIParent)
  self.frames.list:Hide ()
  self.frames.list:SetWidth (200)
  self.frames.list:SetHeight (800)
  self.frames.list:EnableMouse (true)
  self.frames.list:SetMovable (true)
  self.frames.list:RegisterForDrag ("LeftButton")
  self.frames.list:SetScript ("OnDragStart", function () this:StartMoving () end)
  self.frames.list:SetScript ("OnDragStop", function () this:StopMovingOrSizing () end)
  self.frames.list:SetBackdrop (
  {
      bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background", tile = true, tileSize = 16,
      edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", edgeSize = 16,
      insets =
      {
        left = 4,
        right = 4,
        top = 4,
        bottom = 4
      },
  })
  self.frames.list:SetBackdropBorderColor (.5, .5, .5)
  self.frames.list:SetBackdropColor (0,0,0)
  self.frames.list:ClearAllPoints ()
  self.frames.list:SetPoint ("CENTER", WorldFrame, "CENTER", 0, 0)

  self.frames.list.names = {}  
end

-- Called upon PLAYER_LOGIN / Enabling
function InterruptAlert:OnEnable ()
  self:RegisterEvent ("CHAT_MSG_SPELL_SELF_DAMAGE", "CheckInterrupt")
  self:RegisterEvent ("CHAT_MSG_SPELL_FRIENDLYPLAYER_DAMAGE", "CheckInterrupt")
  self:RegisterEvent ("CHAT_MSG_SPELL_PARTY_DAMAGE", "CheckInterrupt")
end

-- Called upon disabling
function InterruptAlert:OnDisable ()
  self:UnregisterEvent ("CHAT_MSG_SPELL_SELF_DAMAGE")
  self:UnregisterEvent ("CHAT_MSG_SPELL_FRIENDLYPLAYER_DAMAGE")
  self:UnregisterEvent ("CHAT_MSG_SPELL_PARTY_DAMAGE")
end

function InterruptAlert:CheckInterrupt (message)
  local start, _, player, target, spell = string.find (message, "(%a+) interrupt[s]? ([%a%s]+)'s ([%a%s]+)%.")

  if (start ~= nil) then
    if (player == "You") then
      player = UnitName ("player")
    end
    
    self:AddInterrupt (player, target, spell)
  
    DEFAULT_CHAT_FRAME:AddMessage ("[" .. player .. "] interrupts [" .. spell .. "]")
  end
end

function InterruptAlert:AddInterrupt (player, target, spell)
  local i = (#self.frames.list.names or 0) + 1
  local normalFont, _, _ = GameFontNormal:GetFont ()

  self.frames.list.names[i] = {}

  self.frames.list.names[i].details = self.frames.list:CreateFontString (nil, "OVERLAY")
  self.frames.list.names[i].details:SetFont (normalFont, 10)
  self.frames.list.names[i].details:SetJustifyH ("LEFT")
  self.frames.list.names[i].details:SetJustifyV ("TOP")
  self.frames.list.names[i].details:SetText (player .. " - " .. target .. " - " .. spell)
  self.frames.list.names[i].details:ClearAllPoints ()
  
  self.frames.list.names[i].details:SetPoint ("TOPLEFT", self.frames.list, "TOPLEFT", 10, (-12 * i))
end